package com.atguigu.realtime.app;

import com.atguigu.realtime.common.Constant;
import com.atguigu.realtime.util.SQLUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import static org.apache.flink.streaming.api.environment.CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION;

/**
 * @Author lzc
 * @Date 2023/3/13 08:58
 */
public abstract class BaseSqlApp {
    public void init(int port, int p, String ckAndJobName) {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);
        
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(p);
        
        // 1.开启 checkpoint
        env.enableCheckpointing(3000);
        // 2. 设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        // 3. 给 checkpoint 做一些配置
        // 3.1 设置模式
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        // 3.2 设置 checkpoint 的超时时间
        env.getCheckpointConfig().setCheckpointTimeout(60 * 1000);
        // 3.3 设置 checkpoint 的并发数
        //        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
        // 3.4 两个 checkpoint 的之间的最小时间间隔
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);
        // 3.5 当 job 取消的时候, 是否删除 checkpoint 的数据
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(RETAIN_ON_CANCELLATION);
        // 3.6 checkpoint的位置
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/gmall2022/" + ckAndJobName);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        // 给 sql应用设置 jobname
        tEnv.getConfig().getConfiguration().setString("pipeline.name", ckAndJobName);
        
        // tEnv.executeSql("增删改 ddl");
        // tEnv.sqlQuery("查询");
        handle(env, tEnv);
        
        
    }
    
    public abstract void handle(StreamExecutionEnvironment env,
                                StreamTableEnvironment tEnv);
    
    public void readOdsDb(StreamTableEnvironment tEnv, String groupId){
        tEnv.executeSql("create table ods_db(" +
                            " `database` string, " +
                            " `table` string, " +
                            " `type` string, " +
                            " `data` map<string, string>, " +
                            " `old` map<string, string>, " +
                            " `ts` bigint, " +
                            " `pt` as proctime(), " +
                            " et as to_timestamp_ltz(ts, 0)," +
                            " watermark for et as et - interval '3' second " +
                            ")" + SQLUtil.getKafkaSourceDDL(Constant.TOPIC_ODS_DB, groupId));
        // pt as fun(): 是在 ddl 中定义字段的时候使用
        
        // fun() as f: 是在 sql 查询中使用  date_format(192, 'yyyy') as df
    }
    
    public void readBaseDic(StreamTableEnvironment tEnv){
        tEnv.executeSql("create table base_dic(" +
                            " dic_code string, " +
                            " dic_name string " +
                            ")with(" +
                            " 'connector' = 'jdbc'," +
                            " 'url' = 'jdbc:mysql://hadoop162:3306/gmall2022?useSSL=false'," +
                            " 'table-name' = 'base_dic', " +
                            " 'username' = 'root', " +
                            " 'password' = 'aaaaaa' ," +
                            " 'lookup.cache.ttl' = '2 hour',  " +  // 把查到维度数据存入到缓存的时间
                            " 'lookup.cache.max-rows' = '20'  " +  // 最多缓存 20 行
                            ")");
    }
    
}
